/*
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using BaseTools.Configuration;

namespace BaseTools.Searching
{
	/// <summary>
	/// Validates pairs
	/// </summary>
	public class PairValidator : IValidator<ConfigPair>
	{
		private string check;
		private bool casesens;
		private bool acceptSection, acceptString;
		
		/// <summary>
		/// Creates a new string validator
		/// </summary>
		/// <param name="check">The string to check for</param>
		public PairValidator(string check)
		{
			this.check = check;
			this.casesens = true;
			this.acceptSection = true;
			this.acceptString = true;
		}
		
		/// <summary>
		/// Creates a new string validator
		/// </summary>
		/// <param name="check">The string to check for</param>
		/// <param name="casesens">Whether the comparison is case-sensitive</param>
		public PairValidator(string check, bool casesens)
		{
			this.check = check;
			this.casesens = casesens;
			this.acceptSection = true;
			this.acceptString = true;
		}
		
		/// <summary>
		/// Creates a new string validator
		/// </summary>
		/// <param name="check">The string to check for</param>
		/// <param name="casesens">Whether the comparison is case-sensitive</param>
		/// <param name="acceptSection">Whether sections are accepted</param>
		/// <param name="acceptString">Whether strings are accepted</param>
		public PairValidator(string check, bool casesens, bool acceptSection, bool acceptString)
		{
			this.check = check;
			this.casesens = casesens;
			this.acceptSection = acceptSection;
			this.acceptString = acceptString;
		}
		
		/// <summary>
		/// The string used to compare for validity
		/// </summary>
		public string Check
		{
			get
			{
				return this.check;
			}
		}
		
		/// <summary>
		/// Whether the check is case-sensitive
		/// </summary>
		public bool CaseSensitive
		{
			get
			{
				return this.casesens;
			}
		}
		
		/// <summary>
		/// Whether sections are accepted
		/// </summary>
		public bool AcceptSection
		{
			get
			{
				return this.acceptSection;
			}
		}
		
		/// <summary>
		/// Whether strings are accepted
		/// </summary>
		public bool AcceptString
		{
			get
			{
				return this.acceptString;
			}
		}
		
		/// <summary>
		/// Checks that the string is the same as the one defined in the constructor
		/// </summary>
		/// <param name="comp">The string to compare</param>
		/// <returns>Whether comp is the same as Check</returns>
		public bool ItemValid(ConfigPair comp)
		{
			if ((comp.DataIsList && !acceptSection) || (comp.DataIsString && !acceptString))
				return false;
			
			if (casesens)
				return string.Compare(comp.Key, this.check) == 0;
			else
				return string.Compare(comp.Key, this.check, true) == 0;
		}
	}
}
